import { _decorator, Component, EditBox, Label, log, Node, ScrollView } from 'cc';
// import { ProtobufWebSocketClient } from './ProtobufWebSocketClient';
// import * as WebSocket from 'ws';

const { ccclass, property } = _decorator;

@ccclass('ProToBTest')
export class ProToBTest extends Component {
    
    
    @property(EditBox)
    private textNode: EditBox = null;

    @property(Label)
    private ChatFrame: Label = null;

    @property(ScrollView)
    private scrollView: ScrollView = null;
    
    // 使用客户端
    // private socket = new ProtobufWebSocketClient('ws://127.0.0.1:6666/websocket');
    private socket = null;
    
    start() {
        this.ChatFrame.getComponent(Label).string = '';

        // let proto = new ProtobufWebSocketClient();

        // log('proto', proto)

        // proto.show()
        
        // const userToSend :proto.chatMessage = new proto.chatMessage();
        // log('userToSend', userToSend)
        
    }

    update(deltaTime: number) {
        
    }

    // 自定义函数处理私聊消息
    handlePrivateMessage(message) {
        console.log(`Received private message from ${message.from}: ${message.content}`);
        this.ChatFrame.getComponent(Label).string += '\n' + message.content;
    }

    // 自定义函数处理群聊消息
    handleGroupMessage(message) {
        console.log(`Received group message: ${message.content}`);
    }

    sendMessage() {
        let message = this.textNode.getComponent(EditBox).string;

        // const userToSend :ChatMessage.chatMessage = new ChatMessage.chatMessage();
        // userToSend.setId(123);
        // userToSend.setName('John Doe');
        // userToSend.setEmail('john.doe@example.com');

        // this.socket.sendMessage(userToSend);

        // if (this.socket.readyState === WebSocket.OPEN) {
        //     let message = this.textNode.getComponent(EditBox).string;
        //     log("发送消息：" + message);

        //     var customMessage = {
        //         type: 'private',
        //         from: 'user1',
        //         to: 'user2',
        //         content: message
        //     };
        //     this.socket.send(JSON.stringify(customMessage));
        //     this.scrollView.scrollToBottom(0.2);
        // }
    }
}


